package xjtu.se.articlecommunity.user.articledetail.impl;

import com.vdurmont.emoji.EmojiParser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.se.articlecommunity.user.articledetail.ArticleDetailService;
import xjtu.se.articlecommunity.user.articledetail.dao.ArticleDetailDao;
import xjtu.se.articlecommunity.user.articledetail.dto.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
@Transactional
public class ArticleDetailServiceImpl implements ArticleDetailService {
    @Resource
    private ArticleDetailDao articleDetailDao;

    @Override
    public Article getArticleInfo(String u_name) {
        Article article=articleDetailDao.getInfo(u_name);
        return article;
    }

    @Override
    public int userLiking(String u_id, String a_id) {
        int ifexist=articleDetailDao.getLikingState(u_id,a_id);

        if(ifexist==1){
            int ifchecked=articleDetailDao.getIfChecked(u_id,a_id);
            if(ifchecked==1){
                ifchecked=0;
            }else{
                ifchecked=1;
            }
            articleDetailDao.updateRecord(u_id,a_id,ifchecked);
            return ifchecked;
        }else{
            articleDetailDao.insertRecord(u_id,a_id,1);
        }

        return 1;
    }


    @Override
    public String comment(CommentDto commentDto) {
        while(true){
            int c_id=new Random().nextInt(10000);
            String idString=Integer.toString(c_id);
            int c_idNum=articleDetailDao.getC_idNum(idString);
            if(c_idNum==0){
                String id=Integer.toString(c_id);
                articleDetailDao.insertComment(commentDto.getCommentcontent(),commentDto.getU_id(),commentDto.getA_id(),id);
                return  id;
            }
        }

    }

    @Override
    public List<User> getLikingUsers(String a_id) {
        return articleDetailDao.getLikingUsers(a_id);
    }

    @Override
    public ArrayList<CommentInfo> getCommentInfo(String u_id,String a_id) {
        ArrayList<CommentInfo> list=articleDetailDao.getCommnetInfo(a_id,0);
        ArrayList<CommentInfo> children=articleDetailDao.getCommnetInfo(a_id,1);
        for(CommentInfo commentInfo:list){
            if(articleDetailDao.ifCommentOrRely(u_id,commentInfo.getC_id())==1){
                commentInfo.setLike(true);
            }else{
                commentInfo.setLike(false);
            }
            commentInfo.setCommentLikeNum(articleDetailDao.getCommentLikeNum(commentInfo.getC_id()));
            for(CommentInfo commentInfo1:children){
                if(articleDetailDao.ifCommentOrRely(u_id,commentInfo1.getC_id())==1){
                    commentInfo1.setLike(true);
                }else{
                    commentInfo1.setLike(false);
                }
                commentInfo1.setCommentLikeNum(articleDetailDao.getCommentLikeNum(commentInfo1.getC_id()));
                commentInfo1.setBu_name(articleDetailDao.getBu_name(commentInfo1.getFc_id()));
                if(commentInfo1.getGrandfc_id().equals(commentInfo.getC_id())){
                    commentInfo.getChildren().add(commentInfo1);
                }
            }
        }

        return  list;
    }

    @Override
    public List<String> getUserTab(String a_id) {
        return articleDetailDao.getUserTab(a_id);
    }

    @Override
    public String[] getArticleTabs(String a_id) {

        return articleDetailDao.getArticleTabs(a_id);
    }

    @Override
    public String reply(String reply_detail, String u_id, String c_id) {
        ParentComment parentComment=articleDetailDao.getParentCommentByC_id(c_id);
        String grandfc_id ;
        String newC_id;
        while(true) {
            int newC_idNum = new Random().nextInt(1000) + 1;
            newC_id=Integer.toString(newC_idNum);
            if(articleDetailDao.ifExist(newC_id)==0) {
                break;
            }
        }
        if(parentComment.getIf_fc_id()==0){
            grandfc_id=c_id;
        }else{
            grandfc_id=parentComment.getGrandfc_id();
        }
        articleDetailDao.insertReply(newC_id,parentComment.getC_id(),parentComment.getA_id(),u_id,reply_detail,grandfc_id);
        return newC_id;
    }

    @Override
    public int likeComment(LikeCommentDto commentDto) {
        int ifexist=articleDetailDao.getReplyLikingState(commentDto.getU_id(),commentDto.getC_id());

        if(ifexist==1){
            int ifchecked=articleDetailDao.getReplyIfChecked(commentDto.getU_id(),commentDto.getC_id());
            if(ifchecked==1){
                ifchecked=0;
            }else{
                ifchecked=1;
            }
            articleDetailDao.updateReplyRecord(commentDto.getU_id(),commentDto.getC_id(),ifchecked);
            return ifchecked;
        }else{
            articleDetailDao.insertReplyRecord(commentDto.getU_id(),commentDto.getC_id(),1);
        }

        return 1;
    }

    @Override
    public int attend(LikeArticleDto dto) {
        String u_id=articleDetailDao.getU_idByA_id(dto.getA_id());
        int ifexist=articleDetailDao.getAttendState(dto.getU_id(),u_id);

        if(ifexist==1){
            int ifchecked=articleDetailDao.getAttendIfChecked(dto.getU_id(),u_id);
            if(ifchecked==1){
                ifchecked=0;
            }else{
                ifchecked=1;
            }
            articleDetailDao.updateAttendRecord(dto.getU_id(),u_id,ifchecked);
            return ifchecked;
        }else{
            articleDetailDao.insertAttendRecord(dto.getU_id(),u_id,1);
        }
        return 1;
    }

    @Override
    public int collect(LikeArticleDto dto) {
            int ifexist=articleDetailDao.getCollectState(dto.getU_id(),dto.getA_id());

        if(ifexist==1){
            int ifchecked=articleDetailDao.getCollectIfChecked(dto.getU_id(),dto.getA_id());
            if(ifchecked==1){
                ifchecked=0;
            }else{
                ifchecked=1;
            }
            articleDetailDao.updateCollectRecord(dto.getU_id(),dto.getA_id(),ifchecked);
            return ifchecked;
        }else{
            articleDetailDao.insertCollectRecord(dto.getU_id(),dto.getA_id(),1);
        }
        return 1;
    }

    @Override
    public int getCollectNum(String a_id) {
        return articleDetailDao.getCollectNumByA_id(a_id);
    }

    @Override
    public int getIfLikeArticle(String u_id, String a_id) {
        if(articleDetailDao.getLikingState(u_id,a_id)==1&&articleDetailDao.getIfChecked(u_id,a_id)==1){
            return 1;
        }
        return 0;
    }

    @Override
    public int getIfCollectArticle(String u_id, String a_id) {
        if(articleDetailDao.getCollectState(u_id,a_id)==1&&articleDetailDao.getCollectIfChecked(u_id,a_id)==1){
            return 1;
        }
        return 0;
    }

    @Override
    public int getIfAttendUser(String u_id, String a_id) {
        String befollowed_u_id=articleDetailDao.getU_idByA_id(a_id);
        if(articleDetailDao.getAttendState(u_id,befollowed_u_id)==1&&articleDetailDao.getAttendIfChecked(u_id,befollowed_u_id)==1){
            return 1;
        }
        return 0;
    }



    @Override
    public Article getArticle(String a_id) {
        Article article=articleDetailDao.getArticle(a_id);
        String html = EmojiParser.parseToUnicode(article.getA_html());
        article.setA_html(html);
        return article;
    }


}
